package com.vito.framework.system.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.vito.framework.base.exception.CommonException;
import com.vito.framework.base.service.impl.BaseServiceImpl;
import com.vito.framework.system.group.mapper.GroupMapper;
import com.vito.framework.system.group.model.GroupBean;
import com.vito.framework.system.group.service.GroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GroupServiceImpl extends BaseServiceImpl<GroupMapper, GroupBean> implements GroupService{

	@Resource
	GroupMapper groupMapper;

    @Override
    public List<GroupBean> queryParentsList(Map<String, Object> map) throws CommonException {
        return groupMapper.queryParentsList(map);
    }

    @Override
    public List<GroupBean> queryParentsTreeList(Map<String, Object> map) throws CommonException {
        List<GroupBean> groupList = queryParentsList(map);
        return generateTree(groupList);
    }

    @Override
    public List<GroupBean> queryGroupTreeList(Map<String, Object> map) throws CommonException {
        List<GroupBean> groupList = list(new LambdaQueryWrapper<GroupBean>().eq(GroupBean::getType, map.get("param_type")).in(GroupBean::getId, map.get("rootPath")));
        return generateTree(groupList);
    }
    
    private List<GroupBean> generateTree(List<GroupBean> groupList) {
        List<GroupBean> topGroupList = new ArrayList<GroupBean>();
        Map<String, List<GroupBean>> childMap = new HashMap<String, List<GroupBean>>();
        for(GroupBean group : groupList){
            if("0".equals(group.getParentId())){
                topGroupList.add(group);
            }
            if (childMap.get(group.getParentId()) == null) {
                List<GroupBean> tempChildList = new ArrayList<GroupBean>();
                tempChildList.add(group);
                childMap.put(group.getParentId(), tempChildList);
            } else {
                childMap.get(group.getParentId()).add(group);
            }
        }
        for (GroupBean top : topGroupList) {
            top.setChildren(getChildrenGroupList(childMap,top.getId()));
        }
        return topGroupList;
    }
    
    /**
     * Create At:   2017年8月25日
     * @author   ztshan
     * @param    
     * @return    
     * @description  递归拼装树
     */
    private List<GroupBean> getChildrenGroupList(Map<String, List<GroupBean>> map, String parentId) {
        List<GroupBean> children = null;
        if (map.get(parentId) != null) {
            for (GroupBean groupBean : map.get(parentId)) {
                groupBean.setChildren(this.getChildrenGroupList(map, groupBean.getId()));
            }
            children = map.get(parentId);
        }
        return children;
    }

    @Override
    public void addGroupSave(GroupBean groupBean) throws CommonException {
        if (groupBean.getParentId() == null) {
            groupBean.setParentId("0");
        }
        GroupBean parentGroup = null;
        String groupPath=null;
        save(groupBean);
//        String groupId = (String) baseDao.selectOne("system.group.getNextGroupId");
        if (!"0".equals(groupBean.getParentId())) {
            parentGroup = getById(groupBean.getParentId());
            groupBean.setType(parentGroup.getType());
            groupPath = parentGroup.getRootPath() + "|" + groupBean.getId();
        } else {
            groupPath = 0 + "|" + groupBean.getId();
        }
        groupBean.setRootPath(groupPath);
        updateById(groupBean);
    }
}
